import Vue from "vue";

import vueRouter from "vue-router";
import routes from "./routes"

Vue.use(vueRouter);


// 获取原来的原型上方法，准备重写，来解决返回promise对象的问题
let originPush = vueRouter.prototype.push;
let originReplace = vueRouter.prototype.replace;

/**
 * 第一个参数：路由地址，包括其参数
 * 第二个参数：promise成功回调
 * 第三个参数：promise失败回调
 * */
vueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    // 用call是因为强迫使this为当前的 originPush，不能是window
    originPush.call(this, location, resolve, reject);
    // 指的是 有resolve、reject就用传过来的否则自己写个空的，反正要保证有
  } else {
    originPush.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};

vueRouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    // 用call是因为强迫使this为当前的 originPush，不能是window
    originReplace.call(this, location, resolve, reject);
  } else {
    originReplace.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};

export default new vueRouter({
  routes,
  // eslint-disable-next-line no-unused-vars
  scrollBehavior(to, from, savedPosition) {
    // 始终滚动到顶部
    return { y: 0 }
  },
});
